//
//  GameListViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2021/12/26.
//

#import "GameListViewController.h"
#import "GameCollectionViewCell.h"
#import "GameListApi.h"
#import "GameModel.h"
#import "GameDetailViewController.h"

@interface GameListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSNumber *typeId;
@property (nonatomic, strong) UICollectionView *collection;
@property(strong,nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *dataSources;

@property (nonatomic, strong) GameListApi *api;

@end

@implementation GameListViewController

- (GameListApi *)api
{
    if (!_api) {
        _api = [[GameListApi alloc] initWithType:self.typeId];
        _api.pageSize = 10;
    }
    return _api;
}

- (NSMutableArray *)dataSources
{
    if (!_dataSources) {
        _dataSources = [NSMutableArray array];
    }
    return _dataSources;
}

- (instancetype)initWithType:(NSNumber *)typeId
{
    self = [super init];
    if (self) {
        self.typeId = typeId;
        self.api.pageNum = 1;
        [self fetchData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.centerTitle.text = @"攻略";
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 20;
    flowLayout.minimumInteritemSpacing = 20;
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 20, 15, 20);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collection.backgroundColor = kBgColor;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerNib:[UINib nibWithNibName:@"GameCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"GameCollectionViewCell"];
    [self.view addSubview:self.collection];
    
    __weak typeof(self) weakSelf = self;
    self.collection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        __strong typeof(self) self = weakSelf;
        self.api.pageNum = 1;
        [self fetchData];
        
    }];
    self.collection.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        __strong typeof(self) self = weakSelf;
        self.api.pageNum ++;
        [self fetchData];
    }];
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.collection.frame = CGRectMake(0, 0, SL_WINDOW_WIDTH, self.view.height);
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSources.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GameCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GameCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.dataSources[indexPath.row];
    return cell;
}

#pragma mark - UICollectDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = GameCollectionViewCell_Width();
    CGFloat height = GameCollectionViewCell_Height();
    
    return CGSizeMake(width,height);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GameModel *model = self.dataSources[indexPath.row];
//    GameDetailViewController *vc = [[GameDetailViewController alloc] init];
//    vc.model = model;
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)fetchData
{
    [self.api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.collection.mj_header endRefreshing];
        if (self.api.success) {
            if (self.api.pageNum == 1) {
                [self.dataSources removeAllObjects];
            }
            NSArray *dataArray = [GameModel mj_objectArrayWithKeyValuesArray:self.api.resArr];
            [self.dataSources addObjectsFromArray:dataArray];
            [self.collection reloadData];
        }
        if (self.api.isMore) {
            [self.collection.mj_footer endRefreshing];
        } else {
            [self.collection.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.collection.mj_footer endRefreshing];
        [self.collection.mj_header endRefreshing];
    }];
}

@end
